/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import static com.hvtt.web.connection.Command.sqlStoredProcedure;
import static com.hvtt.web.connection.Command.sqlText;
import com.hvtt.web.connection.WEBConnection;
import static com.hvtt.web.connection.WEBConnection.getConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.controllerUtils.UtilsController;
import com.hvtt.web.models.C_dangtin;
import com.hvtt.web.models.C_dangtin_comment;
import com.hvtt.web.models.C_doixe;
import com.hvtt.web.models.C_dongxe;
import com.hvtt.web.models.C_hangxe;
import com.hvtt.web.models.C_hinhanh_xe;
import com.hvtt.web.models.C_mauxe;
import com.hvtt.web.models.C_thietbi_nhom;
import com.hvtt.web.models.C_xuatxu;
import com.hvtt.web.models.Si_tinh;
import com.hvtt.web.payment.onePay;
import com.hvtt.web.utilities.Utilities;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Types;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpRequest;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class C_DangTinController implements Serializable {

    private WEBConnection DAL = WEBConnection.getInstance();
    private onePay onePay = new onePay();
    private List<C_thietbi_nhom> lstThietBi = new ArrayList();
    private long lIDHinhAnhXe = 0;
    private C_dangtin item = new C_dangtin();
    private List<C_hangxe> lstHangXe = new ArrayList();
    private List<C_hangxe> lstHangXe1 = new ArrayList();
//    private List<C_doixe> lstDoiXe= new ArrayList();
//    private List<C_dongxe> lstDongXe= new ArrayList();

    public C_DangTinController() {
        item = new C_dangtin();
        DAL = new WEBConnection();
        onePay = new onePay();

        item.setLienHe_DienThoai(Sys_NguoiDungController.findUser().getDienThoai());
        item.setLienHe_Email(Sys_NguoiDungController.findUser().getEmail());
        item.setLienHe_Ho(Sys_NguoiDungController.findUser().getHo());
        item.setLienHe_Ten(Sys_NguoiDungController.findUser().getTen());

    }

    public C_dangtin getItem() {
        if (this.item.getHinhAnh().size() < this.item.getSoAnhToiDa()) {
            int i = this.item.getHinhAnh().size();
            while (i < this.item.getSoAnhToiDa()) {
                C_hinhanh_xe c = new C_hinhanh_xe();
                c.setID(-i);

                this.item.getHinhAnh().add(c);
                i++;
            }
        }
        return item;
    }

    public void setItem(C_dangtin item) {
        this.item = item;
    }

    public List<C_dangtin> findAllBangTin() throws SQLException {
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(Sys_NguoiDungController.findUser().getUserID());
        List<C_dangtin> lst = DAL.executeQuery("Select* from c_dangtin where c_dangtin.UserID=? and c_dangtin.Is_UserDelete<>1", Command.sqlText, Params, C_dangtin.class);
        return lst;
    }

    public List<C_hangxe> findAllHangXe() throws SQLException {
        if (Sys_NguoiDungController.findUser().getLoai() == 0) {
            WEBParamCollection Params = WEBParamCollection.getInstance();
            Params.add(Sys_NguoiDungController.findUser().getLoaiXeBan());
            lstHangXe = DAL.executeQuery("Select* from C_HangXe where C_HangXe.TrangThai=1 and FIND_IN_SET(C_HangXe.ID,?)", Command.sqlText, Params, C_hangxe.class);
        } else {
            if (lstHangXe.size() <= 0) {
                lstHangXe = DAL.executeQuery("Select* from C_HangXe where C_HangXe.TrangThai=1", C_hangxe.class);
            }
        }
        if (lstHangXe.size() > 0 && this.getItem().getHangXeID() <= 0) {
            this.getItem().setHangXeID(lstHangXe.get(0).getID());
        }
        return lstHangXe;

    }
    public List<C_hangxe> findAllHangXe1() throws SQLException {
//        if (lstHangXe1.size() <= 0) {
//                
//            }
        lstHangXe1 = DAL.executeQuery("Select* from C_HangXe where C_HangXe.TrangThai=1", C_hangxe.class);
//        String[]M=Sys_NguoiDungController.findUser().getLoaiXeBan().split(",");
//        for(String s:M)
//        {
//            for(int i=0;i<lstHangXe1.size();i++)
//            {
//                if(!"".equals(s.trim()) && lstHangXe1.get(i).getID()==Long.parseLong(s))
//                {
//                    lstHangXe1.get(i).setSelected("selected");
//                    break;
//                }
//                
//            }
//        }
        
//        if (lstHangXe.size() > 0 && this.getItem().getHangXeID() <= 0) {
//            this.getItem().setHangXeID(lstHangXe.get(0).getID());
//        }
        return lstHangXe1;

    }

    public List<C_thietbi_nhom> findAllThietBi() throws SQLException {
        if (lstThietBi.size() <= 0) {
            WEBParamCollection Params = WEBParamCollection.getInstance();
            Params.add(this.getItem().getID());

            List<C_thietbi_nhom> lst = DAL.executeQuery("{call PG_Load_C_ThietBi_All(?)}", sqlStoredProcedure, Params, C_thietbi_nhom.class);
            List<C_thietbi_nhom> lstResult = new ArrayList<>();
            int i = 0;
            while (i < lst.size()) {
                C_thietbi_nhom c = lst.get(i);
                i++;
                int j = i;
                while (j < lst.size()) {
                    if (lst.get(j).getNhomID() == c.getNhomID()) {
                        c.getItems().add(lst.get(j));
                        i++;
                    }
                    j++;
                }
                lstResult.add(c);
                i++;
            }
            lstThietBi = lstResult;

            return lstResult;
        }
        return lstThietBi;
    }

    public List<C_doixe> findAllDoiXe() throws SQLException {
        if (this.getItem().getHangXeID() > 0) {

            WEBParamCollection Params = new WEBParamCollection();
            Params.add(this.getItem().getHangXeID());
            List<C_doixe> lst = DAL.executeQuery("Select* from C_doixe where C_doixe.HangXeID=? and C_doixe.TrangThai=1", Command.sqlText, Params, C_doixe.class);
            if (lst != null && lst.size() > 0) {
                return lst;
            }
        }
        return new ArrayList<>();
    }

    public List<C_xuatxu> findAllXuatXu() throws SQLException {
        List<C_xuatxu> lst = DAL.executeQuery("Select* from C_XuatXu where C_XuatXu.TrangThai=1", C_xuatxu.class);

        return lst;
    }

    public List<C_dongxe> findAllDongXe() throws SQLException {
        List<C_dongxe> lst = DAL.executeQuery("Select* from C_DongXe where C_DongXe.TrangThai=1", C_dongxe.class);

        return lst;
    }

    public List<C_mauxe> findAllMauXe() throws SQLException {
        List<C_mauxe> lst = DAL.executeQuery("Select* from C_MauXe where C_MauXe.TrangThai=1", C_mauxe.class);

        return lst;
    }

    public List<String> findAllNamSanXuat() {
        List<String> lst = new ArrayList<>();

        int iYear = Calendar.getInstance().get(Calendar.YEAR);
        for (int i = iYear; i >= iYear - 40; i--) {
            lst.add(String.valueOf(i));
        }
        return lst;
    }

    private String validateFields() throws SQLException {
        String str = "";
        if (this.getItem().getHangXeID() <= 0) {
            str += "  + " + Utilities.getLanguages("HangXe") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if (this.getItem().getDoiXeID() <= 0) {
            str += "  + " + Utilities.getLanguages("DoiXe") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if (this.getItem().getNamSanXuat() <= 0) {
            str += "  + " + Utilities.getLanguages("NamSanXuat") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
//        if (this.getItem().getXuatXuID()<=0)
//        {
//            str+= "  + " + Utilities.getLanguages("XuatXu")+": " + Utilities.getLanguages("Require") + "<br/>";
//        }
        if (this.getItem().getGia() <= 0) {
            str += "  + " + Utilities.getLanguages("GiaBan") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if ("".equals(this.getItem().getTienTeID().trim())) {
            str += "  + " + Utilities.getLanguages("TienTe") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
//        if (this.getItem().getSoChoNgoi()<=0)
//        {
//            str+= "  + " + Utilities.getLanguages("SoChoNgoi")+": " + Utilities.getLanguages("Require") + "<br/>";
//        }
        if (this.getItem().getTuKhoaTimKiem().trim() == "1") {
            if (this.getItem().getSoCua() <= 0) {
                str += "  + " + Utilities.getLanguages("SoCua") + ": " + Utilities.getLanguages("Require") + "<br/>";
            }
        }
        if ("".equals(this.getItem().getLienHe_Ho().trim())) {
            str += "  + " + Utilities.getLanguages("Ho") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if ("".equals(this.getItem().getLienHe_Ten().trim())) {
            str += "  + " + Utilities.getLanguages("Ten") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if ("".equals(this.getItem().getLienHe_Email().trim())) {
            str += "  + " + Utilities.getLanguages("Email") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if ("".equals(this.getItem().getLienHe_DienThoai().trim())) {
            str += "  + " + Utilities.getLanguages("DienThoai") + ": " + Utilities.getLanguages("Require") + "<br/>";
        }

        if ("".equals(str.trim())) {
            return "";
        }
        return "<b style='color:red;'>" + str + "</b>";
    }

    private void update_RequestParameter_C_DangTin() {
        this.item.setHangXeID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("HangXeID")));
        this.item.setCao_KichThuoc(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Cao_KichThuoc")));
        this.item.setChieuDaiCoSo(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ChieuDaiCoSo")));
        this.item.setChieuRongCoSo(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ChieuRongCoSo")));
        this.item.setDai_KichThuoc(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Dai_KichThuoc")));
        this.item.setDoiXeID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DoiXeID")));
        this.item.setDongCo(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DongCo")));
        this.item.setDongXeID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DongXeID")));
        this.item.setDungTichBinhChuaNhienLieu(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DungTichBinhChuaNhienLieu")));
        this.item.setDungTichXiLanh(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DungTichXiLanh")));
        this.item.setGia(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Gia").toString().replace(",", "").toString()));
        this.item.setGiamSoc(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("GiamSoc")));
        this.item.setHeThongNapNhienLieu(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("HeThongNapNhienLieu")));
        this.item.setHopSo(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("HopSo")));
        this.item.setKhuVucDang_Tinh(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("KhuVucDang_Tinh")));
        this.item.setKhuVucDang_QuocGia(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("KhuVucDang_QuocGia")));
        this.item.setKieuDanDong(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("KieuDanDong")));
        this.item.setKieuDongCo(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("KieuDongCo")));
        this.item.setLoaiGia(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LoaiGia")));
        this.item.setLopXe(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LopXe")));
        this.item.setMauKhiXuatXuong(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("MauKhiXuatXuong")));
        this.item.setMauNoiThatID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("MauNoiThatID")));
        this.item.setMauXeID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("MauXeID")));
        this.item.setMoTat(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("MoTat")));
        this.item.setMucTieuThuNhienLieu(Double.parseDouble(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("MucTieuThuNhienLieu")));
        this.item.setNamSanXuat(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NamSanXuat")));
        //this.item.setNguoiDang(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NguoiDang")));
        this.item.setNoiThat(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NoiThat")));
        this.item.setPhanHangDoiXe(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("PhanHangDoiXe")));
        this.item.setPhanh(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("Phanh")));
        this.item.setQuyTrinhVanChuyen(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuyTrinhVanChuyen")));
        this.item.setSoChoNgoi(Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("SoChoNgoi")));
        this.item.setSoCua(Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("SoCua")));
        this.item.setSoDaDiKM(Double.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("SoDaDiKM")));
        this.item.setSoNguoiDaSoHuu(Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("SoNguoiDaSoHuu")));
        this.item.setThongSoKyThuatKhac(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ThongSoKyThuatKhac")));
        this.item.setTienTeID(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TienTeID")));
        this.item.setTieuDe(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TieuDe")));
        this.item.setTinhTrangXe(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TinhTrangXe")));
        this.item.setTrongLuong(Double.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TrongLuong")));
        this.item.setTrongLuongKhongTai(Double.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("TrongLuongKhongTai")));
        this.item.setVanhMam(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("VanhMam")));
        this.item.setXuatXuID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("XuatXuID")));

        this.item.setLienHe_Ho(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LienHe_Ho")));
        this.item.setLienHe_Ten(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LienHe_Ten")));
        this.item.setLienHe_Email(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LienHe_Email")));
        this.item.setLienHe_DienThoai(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("LienHe_DienThoai")));
        this.item.setSoVIN(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("SoVIN")));
    }

    @SuppressWarnings("UnusedAssignment")
    public void releaseConnection(ResultSet rs, PreparedStatement pt, CallableStatement call, Connection conn) throws SQLException {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException sqlex) {
                sqlex.printStackTrace();
            }

            rs = null;
        }
        if (call != null) {
            try {
                call.close();
            } catch (SQLException sqlex) {
                sqlex.printStackTrace();
            }

            call = null;
        }

        if (pt != null) {
            try {
                pt.close();
            } catch (SQLException sqlex) {
                sqlex.printStackTrace();
            }

            pt = null;
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException sqlex) {
                sqlex.printStackTrace();
            }
            conn = null;
        }

    }

    public boolean executeUpdate(String sql) throws SQLException {
        boolean bUpdate = false;
        PreparedStatement pt = null;
        CallableStatement call = null;
        Connection conn = null;
        Savepoint savepoint = null;
        try {
            conn = getConnection();

            savepoint = conn.setSavepoint(UUID.randomUUID().toString());
            call = conn.prepareCall(sql);
            call.setObject(1, "Save");
            call.setObject(2, this.item.getID());
            call.registerOutParameter(2, Types.BIGINT);
            call.setObject(3, this.item.getNhomID());
            call.setObject(4, this.item.getMauTinID());
            call.setObject(5, this.item.getURL());
            call.setObject(6, this.item.getTieuDe());
            call.setObject(7, this.item.getNoiDung());
            call.setObject(8, this.item.getGia());
            call.setObject(9, this.item.getLoaiGia());
            call.setObject(10, this.item.getTienTeID());
            call.setObject(11, this.item.getDoiTuongDang());
            call.setObject(12, this.item.getNguoiDang());
            call.setObject(13, this.item.getLineHe());
            call.setObject(14, this.item.getQuyTrinhVanChuyen());
            call.setObject(15, this.item.getTuKhoaTimKiem());
            call.setObject(16, this.item.getKhuVucDang_QuocGia());
            call.setObject(17, this.item.getKhuVucDang_Tinh());
            call.setObject(18, this.item.getKhuVucDang_Huyen());
            call.setObject(19, this.item.getHangXeID());
            call.setObject(20, this.item.getDoiXeID());
            call.setObject(21, this.item.getDongXeID());
            call.setObject(22, this.item.getTinhTrangXe());
            call.setObject(23, this.item.getSoDaDiKM());
            call.setObject(24, this.item.getKieuDanDong());
            call.setObject(25, this.item.getHopSo());
            call.setObject(26, this.item.getXuatXuID());
            call.setObject(27, this.item.getNamSanXuat());
            call.setObject(28, this.item.getSoNguoiDaSoHuu());
            call.setObject(29, this.item.getMauXeID());
            call.setObject(30, this.item.getMauKhiXuatXuong());
            call.setObject(31, this.item.getSoCua());
            call.setObject(32, this.item.getMauNoiThatID());
            call.setObject(33, this.item.getNoiThat());
            call.setObject(34, this.item.getSoChoNgoi());
            call.setObject(35, this.item.getGheChatLieu());
            call.setObject(36, this.item.getThietBiGiaiTri());
            call.setObject(37, this.item.getThietBiKhac());
            call.setObject(38, this.item.getNhienLieu());
            call.setObject(39, this.item.getHeThongNapNhienLieu());
            call.setObject(40, this.item.getMucTieuThuNhienLieu());
            call.setObject(41, this.item.getDungTichBinhNhienLieu());
            call.setObject(42, this.item.getCongXuat());
            call.setObject(43, this.item.getDonViCongXuat());
            call.setObject(44, this.item.getDungTichBinhChuaNhienLieu());
            call.setObject(45, this.item.getTrongLuong());
            call.setObject(46, this.item.getTrongLuongKhongTai());
            call.setObject(47, this.item.getDai_KichThuoc());
            call.setObject(48, this.item.getRong_KichThuoc());
            call.setObject(49, this.item.getCao_KichThuoc());
            call.setObject(50, this.item.getChieuDaiCoSo());
            call.setObject(51, this.item.getChieuRongCoSo());
            call.setObject(52, this.item.getDongCo());
            call.setObject(53, this.item.getKieuDongCo());
            call.setObject(54, this.item.getDungTichXiLanh());
            call.setObject(55, this.item.getPhanh());
            call.setObject(56, this.item.getGiamSoc());
            call.setObject(57, this.item.getLopXe());
            call.setObject(58, this.item.getVanhMam());
            call.setObject(59, this.item.getThongSoKyThuatKhac());
            call.setObject(60, this.item.getMoTat());
            call.setObject(61, this.item.getNgayHieuLuc());
            call.setObject(62, this.item.getLuotUp());
            call.setObject(63, this.item.getUpTuDong());
            call.setObject(64, this.item.getNgayHetHieuLuc());
            call.setObject(65, this.item.getUserID());
            call.setObject(66, this.item.getLoaiTin());
            call.setObject(67, this.item.getIs_UserDelete());
            call.setObject(68, this.item.getDate_UserDelete());
            call.setObject(69, this.item.getIs_AdminDelete());
            call.setObject(70, this.item.getAdminID());
            call.setObject(71, this.item.getDate_AdminDelete());
            call.setObject(72, this.item.getDuyet());
            call.setObject(73, this.item.getNguoi_Duyet());
            call.setObject(74, this.item.getDate_Duyet());
            call.setObject(75, this.item.getPhanHangDoiXe());
            call.setObject(76, this.item.getLienHe_Ho());
            call.setObject(77, this.item.getLienHe_Ten());
            call.setObject(78, this.item.getLienHe_Email());
            call.setObject(79, this.item.getLienHe_DienThoai());
            call.setObject(80, this.item.getSoVIN());
            call.setObject(81, this.item.getSoAnhToiDa());
            call.setObject(82, this.item.getSoVideoToiDa());
            call.setObject(83, this.item.getGoiDangTinID());


            call.execute();

            this.item.setID(call.getLong(2));
        } catch (SQLException e) {
            throw e;
        } finally {
            try {
                this.releaseConnection(null, pt, call, conn);
            } catch (SQLException ex) {
                Logger.getLogger(WEBConnection.class.getName()).log(Level.SEVERE, "Error release connection.Please check connection for next actions!!", ex);
            }
        }

        return bUpdate;
    }

    private WEBParamCollection addParameter(String strAction) {
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(strAction);
        Params.add(this.item.getID());
        Params.add(this.item.getNhomID());
        Params.add(this.item.getMauTinID());
        Params.add(this.item.getURL());
        Params.add(this.item.getTieuDe());
        Params.add(this.item.getNoiDung());
        Params.add(this.item.getGia());
        Params.add(this.item.getLoaiGia());
        Params.add(this.item.getTienTeID());
        Params.add(this.item.getDoiTuongDang());
        Params.add(this.item.getNguoiDang());
        Params.add(this.item.getLineHe());
        Params.add(this.item.getQuyTrinhVanChuyen());
        Params.add(this.item.getTuKhoaTimKiem());
        Params.add(this.item.getKhuVucDang_QuocGia());
        Params.add(this.item.getKhuVucDang_Tinh());
        Params.add(this.item.getKhuVucDang_Huyen());
        Params.add(this.item.getHangXeID());
        Params.add(this.item.getDoiXeID());
        Params.add(this.item.getDongXeID());
        Params.add(this.item.getTinhTrangXe());
        Params.add(this.item.getSoDaDiKM());
        Params.add(this.item.getKieuDanDong());
        Params.add(this.item.getHopSo());
        Params.add(this.item.getXuatXuID());
        Params.add(this.item.getNamSanXuat());
        Params.add(this.item.getSoNguoiDaSoHuu());
        Params.add(this.item.getMauXeID());
        Params.add(this.item.getMauKhiXuatXuong());
        Params.add(this.item.getSoCua());
        Params.add(this.item.getMauNoiThatID());
        Params.add(this.item.getNoiThat());
        Params.add(this.item.getSoChoNgoi());
        Params.add(this.item.getGheChatLieu());
        Params.add(this.item.getThietBiGiaiTri());
        Params.add(this.item.getThietBiKhac());
        Params.add(this.item.getNhienLieu());
        Params.add(this.item.getHeThongNapNhienLieu());
        Params.add(this.item.getMucTieuThuNhienLieu());
        Params.add(this.item.getDungTichBinhNhienLieu());
        Params.add(this.item.getCongXuat());
        Params.add(this.item.getDonViCongXuat());
        Params.add(this.item.getDungTichBinhChuaNhienLieu());
        Params.add(this.item.getTrongLuong());
        Params.add(this.item.getTrongLuongKhongTai());
        Params.add(this.item.getDai_KichThuoc());
        Params.add(this.item.getRong_KichThuoc());
        Params.add(this.item.getCao_KichThuoc());
        Params.add(this.item.getChieuDaiCoSo());
        Params.add(this.item.getChieuRongCoSo());
        Params.add(this.item.getDongCo());
        Params.add(this.item.getKieuDongCo());
        Params.add(this.item.getDungTichXiLanh());
        Params.add(this.item.getPhanh());
        Params.add(this.item.getGiamSoc());
        Params.add(this.item.getLopXe());
        Params.add(this.item.getVanhMam());
        Params.add(this.item.getThongSoKyThuatKhac());
        Params.add(this.item.getMoTat());
        Params.add(this.item.getNgayHieuLuc());
        Params.add(this.item.getLuotUp());
        Params.add(this.item.getUpTuDong());
        Params.add(this.item.getNgayHetHieuLuc());
        Params.add(this.item.getUserID());
        Params.add(this.item.getLoaiTin());
        Params.add(this.item.getIs_UserDelete());
        Params.add(this.item.getDate_UserDelete());
        Params.add(this.item.getIs_AdminDelete());
        Params.add(this.item.getAdminID());
        Params.add(this.item.getDate_AdminDelete());
        Params.add(this.item.getDuyet());
        Params.add(this.item.getNguoi_Duyet());
        Params.add(this.item.getDate_Duyet());
        Params.add(this.item.getPhanHangDoiXe());
        Params.add(this.item.getLienHe_Ho());
        Params.add(this.item.getLienHe_Ten());
        Params.add(this.item.getLienHe_Email());
        Params.add(this.item.getLienHe_DienThoai());
        Params.add(this.item.getSoVIN());
        return Params;
    }

    public List<Si_tinh> findAllTinh() throws SQLException {
        WEBParamCollection Params = new WEBParamCollection();
        Params.add("".equals(this.getItem().getKhuVucDang_QuocGia().trim()) ? "VN" : this.getItem().getKhuVucDang_QuocGia());

        List<Si_tinh> lst = DAL.executeQuery("Select* from SI_Tinh where QuocGiaID=? order by SI_Tinh.ThuTu asc", Command.sqlText, Params, Si_tinh.class);
        return lst;
    }

    public void cmd_getListDoiXe_Click(ActionEvent event) throws IOException, SQLException {
        this.item.setHangXeID(Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("HangXeID").toString()));
        // RequestContext.getCurrentInstance().execute("update=\"pnDoiXe\"");
    }

    public void cmd_getListTinh_Click(ActionEvent event) throws IOException, SQLException {
        this.item.setKhuVucDang_QuocGia(String.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("QuocGiaID").toString()));
        // RequestContext.getCurrentInstance().execute("update=\"pnDoiXe\"");
    }

    public void cmd_Save_Click(ActionEvent event) throws IOException {



        this.getItem().setLoaiTin(1);
        update_RequestParameter_C_DangTin();

        String result = "";
        try {
            result = this.validateFields();
        } catch (SQLException ex) {
            Logger.getLogger(C_DangTinController.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (!result.isEmpty()) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", result);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext.getCurrentInstance().execute("update=\"growl\"");
            return;
        }

        if (this.getItem().getGoiDangTinID() > 1) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/paymentdo.html?money=" + String.valueOf(this.item.getGia()));
            return;
        }

        this.item.setUserID(Sys_NguoiDungController.findUser().getUserID());
        try {
            //addParameter("Save");
            executeUpdate("{call Proc_c_dangtin_Insert_Update(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
        } catch (SQLException ex) {
            Logger.getLogger(C_DangTinController.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext.getCurrentInstance().execute("update=\"growl\"");
            return;
        }

        String strThietBi = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ThietBi").toString();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(this.getItem().getID());
        DAL.executeUpdate("Delete from c_thietbi_xe where IDPhu=? and IDThietBi in (" + strThietBi + ")", Command.sqlText, Params);
        String[] M = strThietBi.split(",");
        for (String s : M) {
            WEBParamCollection Params1 = new WEBParamCollection();
            Params1.add(this.item.getID());
            Params1.add(s);
            DAL.executeUpdate("insert into c_thietbi_xe(IDPhu,IDThietBi) value(?,?)", Command.sqlText, Params1);
        }
        String strHinhAnhChinh = "";
        if (this.item.getHinhAnh().size() > 0) {
            for (C_hinhanh_xe c : this.item.getHinhAnh()) {
                if (c.getTapTin() != null) {
                    String FileName = Utilities.parseDateTime(new Date(), "dd_MM_yyyy_hh_mm_ss") + "_" + c.getHinhAnh();
                    FileUtils.copyInputStreamToFile(c.getTapTin().getInputstream(), new File(Utilities.GetPathUserUploadFile() + File.separator + Sys_NguoiDungController.findUser().getUserID() + File.separator + FileName));

                    if (!"".equals(c.getOldHinhAnh().trim())) {
                        File file = new File(Utilities.GetPathUserUploadFile() + File.separator + Sys_NguoiDungController.findUser().getUserID() + File.separator + c.getOldHinhAnh());
                        file.deleteOnExit();
                    }
                    c.setHinhAnh(FileName);
                    WEBParamCollection Params2 = new WEBParamCollection();
                    Params2.add("Save");
                    Params2.add(c.getID());
                    Params2.add(this.item.getID());
                    Params2.add(c.getHinhAnh());
                    Params2.add(c.getHinhAnhChinh());
                    DAL.executeUpdate("{call Proc_c_HinhAnh_Xe_In_Up_Del(?,?,?,?,?)}", sqlText, Params2);
                    if (c.getHinhAnhChinh()) {
                        strHinhAnhChinh = c.getHinhAnh();
                    }
                }
            }
        }
        if (!"".equals(strHinhAnhChinh.trim())) {
            WEBParamCollection Params3 = new WEBParamCollection();
            Params3.add(strHinhAnhChinh);
            Params3.add(this.item.getID());
            DAL.executeUpdate("update c_dangtin set c_dangtin.HinhAnhChinh=? where c_dangtin.ID=?", sqlText, Params3);
        }

        this.item.resetAttribute();
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/list-ad.html");
        } catch (IOException ex) {
            Logger.getLogger(C_DangTinController.class.getName()).log(Level.SEVERE, null, ex);

            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext.getCurrentInstance().execute("update=\"growl\"");
            return;
        }
    }

    public void cmd_Update_OnPageLoadComplete(ActionEvent event) throws IOException, SQLException {
        RequestContext.getCurrentInstance().execute("update=\":frmdangtin\"");
    }

    public void cmd_Update_Click(ActionEvent event) throws IOException, SQLException {
        this.item.resetAttribute();

        this.getItem().setID(Long.parseLong(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("BangTinID")));


        WEBParamCollection Params = new WEBParamCollection();
        Params.add(this.getItem().getID());
        List<C_dangtin> lst = DAL.executeQuery("Select*from C_DangTin where ID=?", Command.sqlText, Params, C_dangtin.class);
        if (lst != null && lst.size() > 0) {
            this.setItem(lst.get(0));

            WEBParamCollection Params1 = new WEBParamCollection();
            Params1.add(this.getItem().getID());
            List<C_hinhanh_xe> lst1 = DAL.executeQuery("Select*from c_hinhanh_xe where IDPhu=?", Command.sqlText, Params1, C_hinhanh_xe.class);
            this.getItem().setHinhAnh(lst1);
            if("2".equals(this.getItem().getTuKhoaTimKiem()))
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/1/motobike.html");
            else
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/1/ad.html");
            RequestContext.getCurrentInstance().execute("update=\"frmdangtin\"");
        }

    }

    public void cmd_New_Click(ActionEvent event) throws IOException, SQLException {
        this.item.resetAttribute();
//        this.getItem().setGoiDangTinID(2);
//        this.getItem().setSoAnhToiDa(10);
        FacesContext.getCurrentInstance().getExternalContext().redirect("/pricing-and-packages.html");///Users/c/1/ad.html
        RequestContext.getCurrentInstance().execute("update=\"frmdangtin\"");

    }

    public void cmd_SelectPackages(ActionEvent event) throws IOException, SQLException {
        this.item.resetAttribute();
        Map<String, String> m = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.item.setGoiDangTinID(Integer.parseInt(String.valueOf(m.get("GoiDangTinID"))));
        this.item.setSoAnhToiDa(Integer.parseInt(String.valueOf(m.get("SoHinhAnh"))));
        this.item.setTuKhoaTimKiem(String.valueOf(m.get("LoaiXe")));

        if (this.item.getGoiDangTinID() == 2) {
            this.item.setGia(100000);
        } else if (this.item.getGoiDangTinID() == 3) {
            this.item.setGia(200000);
        }

        //FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/1/ad.html");///Users/c/1/ad.html
//        if(this.getItem().getGoiDangTinID()>1)
//            FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/payment.html?money=" + String.valueOf(this.item.getGia()));
//        else
        
         if (!"USERS".equals(Sys_NguoiDungController.findUser().getNhomID().trim().toUpperCase())) {
              FacesContext.getCurrentInstance().getExternalContext().redirect("/login.html");
             return;
        }
        if("2".equals(this.item.getTuKhoaTimKiem()))
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/1/motobike.html");///Users/c/1/ad.html
        else
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/1/ad.html");///Users/c/1/ad.html
            

    }

    public void cmd_Delete_Click(ActionEvent event) throws IOException, SQLException {
        long l = Long.parseLong(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("BangTinID"));

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(l);
        DAL.executeUpdate("{call Proc_c_dangtin_Delete(?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
        this.item.resetAttribute();
        FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/list-ad.html");

    }

    public void cmdSetParamUploadFile_Click(ActionEvent event) throws IOException, SQLException {

        lIDHinhAnhXe = Long.parseLong(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("HinhAnhID"));
    }

    public void handleFileUpload(FileUploadEvent event) throws FileNotFoundException {

        if (this.item.getHinhAnh().size() <= 0) {
            C_hinhanh_xe h = new C_hinhanh_xe();
            h.setTapTin(event.getFile());
            h.setHinhAnh(event.getFile().getFileName());
            h.setHinhAnhChinh(true);
            h.setFileHinhAnh(new DefaultStreamedContent(new ByteArrayInputStream(event.getFile().getContents()), event.getFile().getContentType()));
            this.item.getHinhAnh().add(h);
        } else {
            int iCount = -1;
            for (int i = 0; i < this.item.getHinhAnh().size(); i++) {
                if (this.item.getHinhAnh().get(i).getID() == lIDHinhAnhXe) {
                    iCount = i;
                    break;
                }
            }
            if (iCount < 0) {
                C_hinhanh_xe h = new C_hinhanh_xe();
                h.setTapTin(event.getFile());
                h.setHinhAnh(event.getFile().getFileName());
                h.setHinhAnhChinh(false);
                h.setFileHinhAnh(new DefaultStreamedContent(new ByteArrayInputStream(event.getFile().getContents()), event.getFile().getContentType()));
                this.item.getHinhAnh().add(h);
            } else {
                // C_hinhanh_xe h =new C_hinhanh_xe();
                this.item.getHinhAnh().get(iCount).setTapTin(event.getFile());
                this.item.getHinhAnh().get(iCount).setOldHinhAnh(this.item.getHinhAnh().get(iCount).getHinhAnh());
                this.item.getHinhAnh().get(iCount).setHinhAnh(event.getFile().getFileName());
                this.item.getHinhAnh().get(iCount).setFileHinhAnh(new DefaultStreamedContent(new ByteArrayInputStream(event.getFile().getContents()), event.getFile().getContentType()));
                //this.item.getHinhAnh().get(iCount).setHinhAnhChinh(false);

                // this.item.getHinhAnh().set(iCount, this.item.getHinhAnh().get(iCount));
            }

            lIDHinhAnhXe = 0;
        }

        //event.getFile();

        //this.item.getHinhAnh()
        // this.setUploadedFile(file);
        //this.getVideodemo().setFileName(Utilities.generateFileNameUpload(file.getFileName()))
    }

    public String getFullPathHinhAnh(String hinhanh) {
        UtilsController u = new UtilsController();
        if ("".equals(hinhanh.trim())) {
            return u.getGetURLUserUploadFile() + "/default.jpg";
        }


        File file = new File(Utilities.GetPathUserUploadFile() + File.separator + Sys_NguoiDungController.findUser().getUserID() + File.separator + hinhanh);
        if (file.exists()) {
            return u.getGetURLUserUploadFile() + '/' + Sys_NguoiDungController.findUser().getUserID() + '/' + hinhanh;
        }
        return u.getGetURLUserUploadFile() + "/default.jpg";
    }

    public void cmdChiTietTinNhan_Click(ActionEvent event) throws IOException, SQLException {

        this.item.resetAttribute();

        this.getItem().setID(Long.parseLong(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ID")));


        WEBParamCollection Params = WEBParamCollection.getInstance();
        Params.add(this.getItem().getID());
        List<C_dangtin> lst = DAL.executeQuery("{call PG_Load_c_DangTin_ChiTiet(?)}", Command.sqlStoredProcedure, Params, C_dangtin.class);
        if (lst != null && lst.size() > 0) {

            WEBParamCollection Params2 = WEBParamCollection.getInstance();
            Params2.add(lst.get(0).getID());
            DAL.executeUpdate("Update C_DangTin set C_DangTin.SoNguoiXem=C_DangTin.SoNguoiXem+1 where C_DangTin.ID=?", sqlText, Params2);
            this.setItem(lst.get(0));
            WEBParamCollection Params1 = WEBParamCollection.getInstance();
            Params1.add(this.getItem().getID());
            List<C_hinhanh_xe> lst1 = DAL.executeQuery("Select*from c_hinhanh_xe where IDPhu=?", Command.sqlText, Params1, C_hinhanh_xe.class);
            this.getItem().setHinhAnh(lst1);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/detail.html?id="+this.getItem().getID());
            //RequestContext.getCurrentInstance().execute("update=\"frmdangtin\"");
        }
    }

    public void onPageLoad_ChiTietTinNhan() throws IOException, SQLException {
//        if (this.getItem().getID() <= 0) {
//            FacesContext.getCurrentInstance().getExternalContext().redirect("/index.html");
//        }
        
         this.item.resetAttribute();

        this.getItem().setID(Long.parseLong(Utilities.getRequest().getParameter("id")));


        WEBParamCollection Params = WEBParamCollection.getInstance();
        Params.add(this.getItem().getID());
        List<C_dangtin> lst = DAL.executeQuery("{call PG_Load_c_DangTin_ChiTiet(?)}", Command.sqlStoredProcedure, Params, C_dangtin.class);
        if (lst != null && lst.size() > 0) {
             this.setItem(lst.get(0));
            WEBParamCollection Params2 = WEBParamCollection.getInstance();
            Params2.add(lst.get(0).getID());
            DAL.executeUpdate("Update C_DangTin set C_DangTin.SoNguoiXem=C_DangTin.SoNguoiXem+1 where C_DangTin.ID=?", sqlText, Params2);



           



            WEBParamCollection Params1 = WEBParamCollection.getInstance();
            Params1.add(this.getItem().getID());
            List<C_hinhanh_xe> lst1 = DAL.executeQuery("Select*from c_hinhanh_xe where IDPhu=?", Command.sqlText, Params1, C_hinhanh_xe.class);
            this.getItem().setHinhAnh(lst1);
           // FacesContext.getCurrentInstance().getExternalContext().redirect("/detail.html");
            //RequestContext.getCurrentInstance().execute("update=\"frmdangtin\"");
        }
        
    }

    public void cmdSaveComment_Click(ActionEvent event) throws IOException, SQLException {

        //this.item.resetAttribute();

        String strCommentText = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("CommentText").toString();


        WEBParamCollection Params = new WEBParamCollection();
        Params.add(0);
        Params.add(this.getItem().getID());
        Params.add("".equals(Sys_NguoiDungController.findUser().getUserID().trim()) ? Utilities.getLanguages("Khach") : Sys_NguoiDungController.findUser().getUserID().trim());
        Params.add(strCommentText);
        Params.add("".equals(Sys_NguoiDungController.findUser().getUserID().trim()) ? 0 : 1);
        DAL.executeUpdate("{call Proc_c_dangtin_comment_In_Up_Del(?,?,?,?,?)}", Command.sqlStoredProcedure, Params);
        FacesContext.getCurrentInstance().getExternalContext().redirect("/detail.html?id="+this.getItem().getID());

    }

    public List<C_dangtin_comment> findAllComment() throws SQLException {
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(this.getItem().getID());
        List<C_dangtin_comment> lst = DAL.executeQuery("Select*from c_dangtin_comment where c_dangtin_comment.IDPhu=?", sqlText, Params, C_dangtin_comment.class);
        return lst;
    }

    public boolean executeUpdate_ThanhToanOnline(String sql, onePay p) throws SQLException {
        boolean bUpdate = false;
        PreparedStatement pt = null;
        CallableStatement call = null;
        Connection conn = null;
        Savepoint savepoint = null;
        try {
            conn = getConnection();

            savepoint = conn.setSavepoint(UUID.randomUUID().toString());
            call = conn.prepareCall(sql);

            this.getItem().setGoiDangTinID(1);
            this.getItem().setGia(100);
            this.getItem().setTienTeID("VND");

            call.setObject(1, "".equals(p.getVpc_OrderInfo().trim()) ? 0 : p.getVpc_OrderInfo());
            call.registerOutParameter(1, Types.BIGINT);
            call.setObject(2, "");
            call.setObject(3, p.getVpc_AccessCode());
            call.setObject(4, p.getVpc_MerchTxnRef());
            call.setObject(5, this.item.getGoiDangTinID());
            call.setObject(6, "");
            call.setObject(7, 1);
            call.setObject(8, this.item.getGia());
            call.setObject(9, this.item.getTienTeID());
            call.setObject(10, "");
            call.setObject(11, p.getOnepayURL());
            call.setObject(12, p.getVpc_Merchant());
            call.setObject(13, "");


            call.execute();

            p.setVpc_OrderInfo(call.getString(1));
        } catch (SQLException e) {
            throw e;
        } finally {
            try {
                this.releaseConnection(null, pt, call, conn);
            } catch (SQLException ex) {
                Logger.getLogger(WEBConnection.class.getName()).log(Level.SEVERE, "Error release connection.Please check connection for next actions!!", ex);
            }
        }

        return bUpdate;
    }

    public void onPageLoad_ThanhToanOnline() throws IOException, SQLException {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        //String strReferer = request.getHeader("Referer");
//        request.getRemoteAddr();



        onePay.setVpc_Currency("VND");
        onePay.setVpc_Amount(this.getItem().getGia());
        onePay.setTitle("Ma");
        onePay.setVpc_ReturnURL("http://autoko.co/Users/c/paymentresult.html");

        onePay.setVpc_TicketNo(request.getRemoteAddr());
        // one.setVpc_TicketNo("TEST");
        onePay.setVpc_MerchTxnRef(onePay.find_vpc_MerchTxnRef());


    }

    public void onPageLoad_ThanhToanOnline_Result() throws IOException, SQLException {
        //HttpServletRequest request= (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String strResult = Utilities.getRequest().getParameter("vpc_TxnResponseCode") + ";" + Utilities.getRequest().getParameter("vpc_Message");

        WEBParamCollection Params10 = WEBParamCollection.getInstance();
        Params10.add(strResult);
        Params10.add(Long.parseLong(onePay.getVpc_OrderInfo()));
        DAL.executeUpdate("update p_payment set p_payment.KetQua=?,p_payment.Date_Up=CURRENT_TIMESTAMP() where p_payment.ID=?", sqlText, Params10);

        String str = Utilities.getRequest().getParameter("vpc_TxnResponseCode");
        switch (str) {
            case "0":
                executeUpdate_ThanhToanOnline("{call Proc_P_Payment_In_Up(?,?,?,?,?,?,?,?,?,?,?,?,?)}", onePay);

                this.item.setUserID(Sys_NguoiDungController.findUser().getUserID());
                try {
                    //addParameter("Save");
                    executeUpdate("{call Proc_c_dangtin_Insert_Update(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
                } catch (SQLException ex) {
                    Logger.getLogger(C_DangTinController.class.getName()).log(Level.SEVERE, null, ex);
                    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", ex.getMessage());
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    RequestContext.getCurrentInstance().execute("update=\"growl\"");
                    return;
                }

                String strThietBi = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ThietBi").toString();

                WEBParamCollection Params = WEBParamCollection.getInstance();
                Params.add(this.getItem().getID());
                DAL.executeUpdate("Delete from c_thietbi_xe where IDPhu=? and IDThietBi in (" + strThietBi + ")", Command.sqlText, Params);
                String[] M = strThietBi.split(",");
                for (String s : M) {
                    WEBParamCollection Params1 = new WEBParamCollection();
                    Params1.add(this.item.getID());
                    Params1.add(s);
                    DAL.executeUpdate("insert into c_thietbi_xe(IDPhu,IDThietBi) value(?,?)", Command.sqlText, Params1);
                }
                String strHinhAnhChinh = "";
                if (this.item.getHinhAnh().size() > 0) {
                    for (C_hinhanh_xe c : this.item.getHinhAnh()) {
                        if (c.getTapTin() != null) {
                            String FileName = Utilities.parseDateTime(new Date(), "dd_MM_yyyy_hh_mm_ss") + "_" + c.getHinhAnh();
                            FileUtils.copyInputStreamToFile(c.getTapTin().getInputstream(), new File(Utilities.GetPathUserUploadFile() + File.separator + Sys_NguoiDungController.findUser().getUserID() + File.separator + FileName));

                            if (!"".equals(c.getOldHinhAnh().trim())) {
                                File file = new File(Utilities.GetPathUserUploadFile() + File.separator + Sys_NguoiDungController.findUser().getUserID() + File.separator + c.getOldHinhAnh());
                                file.deleteOnExit();
                            }
                            c.setHinhAnh(FileName);
                            WEBParamCollection Params2 = new WEBParamCollection();
                            Params2.add("Save");
                            Params2.add(c.getID());
                            Params2.add(this.item.getID());
                            Params2.add(c.getHinhAnh());
                            Params2.add(c.getHinhAnhChinh());
                            DAL.executeUpdate("{call Proc_c_HinhAnh_Xe_In_Up_Del(?,?,?,?,?)}", sqlText, Params2);
                            if (c.getHinhAnhChinh()) {
                                strHinhAnhChinh = c.getHinhAnh();
                            }
                        }
                    }
                }
                if (!"".equals(strHinhAnhChinh.trim())) {
                    WEBParamCollection Params3 = new WEBParamCollection();
                    Params3.add(strHinhAnhChinh);
                    Params3.add(this.item.getID());
                    DAL.executeUpdate("update c_dangtin set c_dangtin.HinhAnhChinh=? where c_dangtin.ID=?", sqlText, Params3);
                }

                this.item.resetAttribute();
                try {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/list-ad.html");
                } catch (IOException ex) {
                    Logger.getLogger(C_DangTinController.class.getName()).log(Level.SEVERE, null, ex);

                    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", ex.getMessage());
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    RequestContext.getCurrentInstance().execute("update=\"growl\"");
                    //return;
                }
                //  FacesContext.getCurrentInstance().getExternalContext().redirect("/Users/c/1/ad.html");
                break;
            case "1":
                onePay.setError("Transaction Declined Bank Error");
                break;
            case "2":
                onePay.setError("Bank Declined Transaction");
                break;
            case "3":
                onePay.setError("Transaction Declined - No Reply from Bank");
                break;
            case "4":
                onePay.setError("Transaction Declined Expired Card");
                break;
            case "5":
                onePay.setError("Transaction Declined Insufficient funds");
                break;
            case "6":
                onePay.setError("Transaction Declined Error Communicating with Bank");
                break;
            case "7":
                onePay.setError("Payment Server Processing Error Typically caused by invalid input data such as an invalid credit card number. Processing errors can also occur");
                break;
            case "8":
                onePay.setError("Transaction Declined Transaction Type Not Supported");
                break;
        }

    }

    public String hinhAnhXe(List<C_hinhanh_xe> ha) {
        UtilsController utilsController = Utilities.getManagedBean(UtilsController.class);
        if (ha != null && !ha.isEmpty()) {
            C_hinhanh_xe c = ha.get(0);
            return utilsController.getGetURLUserUploadFile() + "/" + getItem().getUserID() + "/" + c.getHinhAnh();
        }
        return "";
    }

    /**
     * @return the onePay
     */
    public onePay getOnePay() {
        return onePay;
    }

    /**
     * @param onePay the onePay to set
     */
    public void setOnePay(onePay onePay) {
        this.onePay = onePay;
    }
}
